package com.cheyun.netsalev3.viewmodel

import android.view.View
import android.widget.DatePicker
import android.widget.TimePicker
import androidx.appcompat.app.AppCompatActivity
import androidx.databinding.ObservableField
import com.cheyun.netsalev3.R
import com.cheyun.netsalev3.bean.DialogParam
import com.cheyun.netsalev3.bean.dm.DmDealUser
import com.cheyun.netsalev3.repository.DmClueTransferActivityRepository
import com.cheyun.netsalev3.utils.FunctionUtils
import com.cheyun.netsalev3.widget.*
import com.jeremyliao.liveeventbus.LiveEventBus
import java.text.SimpleDateFormat
import java.util.*

class DmClueFollowActivityViewModel(var clueTransferActivityRepository: DmClueTransferActivityRepository,var type:String,var status:String,var isArrive:String) : BaseViewModel() {

    var youxiao : MutableList<DmDealUser> = mutableListOf();
    var daodian : MutableList<DmDealUser> = mutableListOf();

    var youxiaoName = ObservableField<String>("待判定")
    var daodianName = ObservableField<String>("是")

    var youxiaoPosition = 0
    var daodianPosition = 0

    var remark = ObservableField<String>("")
    var time = ObservableField<String>("")
    var clueType = ObservableField<Int>(0)
    var clueStatus = ObservableField<String>("")
    var clueIsArrive = ObservableField<String>("")

    var tt = ""

    init{
        youxiao.add(DmDealUser(0,"待判定"))
        youxiao.add(DmDealUser(2,"有效"))
        youxiao.add(DmDealUser(3,"无效"))

        daodian.add(DmDealUser(1,"是"))
        daodian.add(DmDealUser(0,"否"))

        clueType.set(type.toInt())
        clueStatus.set(status)
        clueIsArrive.set(isArrive)

        if(status.equals("2")){
            youxiaoName.set("有效")
            youxiaoPosition = 1
        }else if(status.equals("3")){
            youxiaoName.set("无效")
            youxiaoPosition = 2
        }
    }

    fun getYouxiao(view: View) {
        if(status.equals("2")){
            MyToast("该线索已判定为有效，无法更改")
            return
        }
        var content = view.context
        if (content is AppCompatActivity) {
            youxiao?.apply {
                var list = ArrayList<DialogParam>()
                for (it in this) {
                    var item = DialogParam(it.name, it.userId.toString())
                    list.add(item)
                }
                var diolog = MyDialog.newInstance("选择有效性", list)
                diolog.huidiaPosition = this@DmClueFollowActivityViewModel::setYouxiao
                diolog.show(content.supportFragmentManager, "missiles2")
            }
        }
    }

    fun setYouxiao(position: Int) {
        youxiaoPosition = position
        if (youxiaoPosition != -1) {
            youxiaoName.set(youxiao!![youxiaoPosition].name)
        }
    }

    fun getDaodian(view: View) {
        var content = view.context
        if (content is AppCompatActivity) {
            daodian?.apply {
                var list = ArrayList<DialogParam>()
                for (it in this) {
                    var item = DialogParam(it.name, it.userId.toString())
                    list.add(item)
                }
                var diolog = MyDialog.newInstance("是否到店", list)
                diolog.huidiaPosition = this@DmClueFollowActivityViewModel::setDaodian
                diolog.show(content.supportFragmentManager, "missiles2")
            }
        }
    }

    fun setDaodian(position: Int) {
        daodianPosition = position
        if (daodianPosition != -1) {
            daodianName.set(daodian!![daodianPosition].name)
        }
    }


    /**
     * 完成
     */
    fun clickBut(view: View) {
        tt = ""
        var s = ""
        if(type.equals("1")){
            //跟进
            if (youxiaoPosition == -1) {
                return
            }
            if (daodianPosition == -1) {
                return
            }
//            if (time.get().isNullOrBlank()) {
//                return
//            }
            s = youxiao?.get(youxiaoPosition)?.userId.toString()
            isArrive = daodian?.get(daodianPosition)?.userId.toString()

        }else{
            s = type
        }
        time.get()?.apply {
            tt = this
        }
        if(tt.equals("")){
            MyToast("请选择跟进时间")
            return;
        }

        if(!clueStatus.get().equals(s) && !s.equals("0")){
            var context = view.context
            if (context is AppCompatActivity) {
                var dialog = MyContentDialogLayout.newInstance("有效性的判断", (if(s.equals("2")) R.layout.dm_clue_status_2 else R.layout.dm_clue_status_3 ))
                dialog.onClickOk = this@DmClueFollowActivityViewModel::confirmStatus
                dialog.show(context.supportFragmentManager, "")
            }
        }else{
            confirmStatus()
        }

//        var method: () -> Unit = {
//            setData()
//        }
//        var error: (String) -> Unit = {
//            MyToast(it)
//        }
      //  clueTransferActivityRepository.clueFollow(status,type, isArrive, t,remark.get()!!, method,error)
    }

    fun confirmStatus(){
        if(type.equals("1")){
            //跟进
            if (youxiaoPosition == -1) {
                return
            }
            if (daodianPosition == -1) {
                return
            }
            status = youxiao?.get(youxiaoPosition)?.userId.toString()
            isArrive = daodian?.get(daodianPosition)?.userId.toString()

        }else{
            status = type
        }
        var method: () -> Unit = {
            setData()
        }
        var error: (String) -> Unit = {
            MyToast(it)
        }
        clueTransferActivityRepository.clueFollow(status,type, isArrive, tt,remark.get()!!, method,error)
    }

    /**
     * 成功回调
     */
    fun setData() {
        LiveEventBus.get().with("dmclueupdatge").postValue("")
        if(type.equals("1")) {
            MyToast("跟进成功")
        }else{
            MyToast("操作成功")
        }
        myFinish();
    }

    var year = ""
    var month = ""
    var day = ""
    var activity: AppCompatActivity? = null

    val simpleDateFormat = SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
    var maxDate = 0L
    /**
     * 选择时间
     */
    fun timeText(view: View) {
        var content = view.context
        if (content is AppCompatActivity) {
            activity = content
            val datePickerFragment = DatePickerFragment()
            val c = Calendar.getInstance()
            datePickerFragment.minDate = c.timeInMillis
            datePickerFragment.maxDate = maxDate
            datePickerFragment.clickOk = this::onDateSet
            datePickerFragment.show(content.supportFragmentManager, "datePicker")
        }
    }


    /**
     * 点击日期回调
     */
    fun onDateSet(view: DatePicker, year: Int, month: Int, day: Int) {
        this.year = year.toString()
        this.month = month.toString()
        this.day = day.toString()
        val timePickerFragment = TimePickerFragment()
        timePickerFragment.clickOk = this::onTimeSet
        timePickerFragment.show(activity?.supportFragmentManager!!, "timePicker")
    }

    /**
     * 点击时间回调
     */
    fun onTimeSet(view: TimePicker, hourOfDay: Int, minute: Int) {
        var t = FunctionUtils.formatTime(year, month, day, hourOfDay, minute)
        time.set(t)
    }


}